package com.example.danci.control;


import com.example.danci.mapper.UserMapper;
import com.example.danci.model.entity.User;
import com.example.danci.model.vo.*;
import com.example.danci.server.IUserService;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")




public class controlUser {
    @Autowired
    private IUserService userService;


@PostMapping("/sign")
    public Result sign(@RequestBody VoUser vouser) {
        int id = vouser.getId();
        String password = vouser.getPassword();
        String username=vouser.getUesrname();
        if(password.length()<8||password.length()>16){
            return   Result.success(null,10001,"fail");
        }
        if(username.length()==0){
            return   Result.success(null,10001,"fail");
        }

         User user = new User();

         BeanUtils.copyProperties(user,vouser);
        boolean flag  =  userService.save(user);
           if(flag) {
               return  Result.success(user,200,"success");
           }
           else {
               return  Result.success(null,10004,"fail");
           }
}


@PostMapping("/login")
    public Result login(@RequestBody VoUser vouser) {


    User user=userService.getById(vouser.getId());
    if(user==null){
        return   Result.success(null,10005,"fail");
    }
    if(!(user.getPassword().equals(vouser.getPassword()))||!user.getUesrname().equals(vouser.getUesrname())){
        return   Result.success(null,10003,"fail");
    }

    return  Result.success(user,200,"success");

}


@DeleteMapping("/signout")
    public Result signout(@RequestBody VoUser vouser) {
    User user=userService.getById(vouser.getId());
    if(user==null){
        return   Result.success(null,10005,"fail");
    }
    if(!(user.getPassword().equals(vouser.getPassword()))||!user.getUesrname().equals(vouser.getUesrname())){
        return   Result.success(null,10003,"fail");
    }

    boolean flag=userService.removeById(user.getId());
    if(flag){
        return  Result.success(user,200,"success");
    }
    else {
        return  Result.success(null,500,"fail");
    }

}

@PutMapping("/set/phone")
    public Result setphone(@RequestBody VoPhone vophone) {

    User user=userService.getById(vophone.getId());
    if(user==null){
        return   Result.success(null,10003,"fail");
    }

   boolean flag= userService.updateByPhone(user.getId(), vophone.getPhone());
    user.setUser_phone(vophone.getPhone());
    if (flag){
        return  Result.success(user,200,"success");
    }
      else {
          return  Result.success(null,500,"fail");
    }
}


    @PutMapping("/set/img")
    public Result setimg(@RequestBody VoMulFile voMulFile) throws IOException {

        User user=userService.getById(voMulFile.getId());
        if(user==null ||voMulFile.getFile().isEmpty()){
            return   Result.success(null,10003,"fail");
        }

        InputStream is=voMulFile.getFile().getInputStream();
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        int len;
        byte[] buff=new byte[1024*10];
        while ((len = is.read(buff)) != -1) {
            out.write(buff, 0, len);
        }

        byte[] imgBytes = out.toByteArray();
        boolean flag = userService.updateByImg(user.getId(), imgBytes);
        user.setUser_img(imgBytes);
        if (flag){
            return  Result.success(user,200,"success");
        }
        else {
            return  Result.success(null,500,"fail");
        }
    }


    @PutMapping("/set/password")
    public Result setpassword(@RequestBody VoPassword voPassword){

        User user=userService.getById(voPassword.getId());
        if(user==null){
            return   Result.success(null,10003,"fail");
        }
        if(!voPassword.getPassword().equals(user.getPassword())||voPassword.getNew_password().length()<8||voPassword.getNew_password().length()>16){
            return   Result.success(null,10004,"fail");
        }
        boolean flag= userService.updateByPassword(user.getId(), voPassword.getPassword());
        user.setPassword(voPassword.getNew_password());
        if (flag){
            return  Result.success(user,200,"success");
        }
        else {
            return  Result.success(null,500,"fail");
        }
    }

    @PutMapping("/set/username")
    public Result setpassword(@RequestBody VoUsername voUsername){

        User user=userService.getById(voUsername.getId());
        if(user==null){
            return   Result.success(null,10003,"fail");
        }
        if(!voUsername.getPassword().equals(user.getPassword())||voUsername.getNew_username().length()==0){
            return   Result.success(null,10004,"fail");
        }
        boolean flag= userService.updateByUsername(user.getId(), voUsername.getNew_username());
         user.setUesrname(voUsername.getNew_username());
        if (flag){
            return  Result.success(user,200,"success");
        }
        else {
            return  Result.success(null,500,"fail");
        }
    }



    @GetMapping("/get/img/{id}")
    public Result getimg(@RequestBody int id) throws IOException{
          User user =userService.getById(id);
        if (user == null) {
            // 用户不存在，返回 404 Not Found
            return Result.success("null", 404, "fail");
        }

        // 获取图像数据
        byte[] imgBytes = user.getUser_img();
        if (imgBytes == null || imgBytes.length == 0) {
            // 图像数据为空，返回 204 No Content
            return Result.success(null, 204, "fail");
        }

        // 将图像数据转换为 Base64 字符串
        String base64Image = Base64.encodeBase64String(imgBytes);

        // 返回图像数据的 Base64 字符串
        return Result.success(base64Image, 200, "success");

    }




}


